package file;

import java.io.RandomAccessFile;
import utility.Input;

public class FileUtility
{
	RandomAccessFile raf;
	
	public FileUtility(RandomAccessFile raf)
	{
	this.raf = raf;
	}
	
	public int getColumnIndex(String columnName)
	{
		long temp=0;
		try
		{
			temp = raf.getFilePointer(); 
			raf.seek(0);
			String xyz=raf.readLine();
			//System.out.println("The Column Herder is : " +xyz);
			System.out.println("The Column for Search is : " +columnName);
			String[]arr1=xyz.split(Input.SEPARATOR);
			for(int i=0;i<arr1.length;i++)
			{
				if(arr1[i].toLowerCase().equals(columnName.toLowerCase()))
				{
					raf.seek(temp);
					return(i+1);
				}
			}
			raf.seek(temp);
			return(0);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return 0;
		
	}
	public String getColumnName(int columnIndex)
	{
		try
		{
			long temp = raf.getFilePointer(); 
			raf.seek(0);
			
			String xyz=raf.readLine();
			String[]arr1=xyz.split("\t");
			
			if (columnIndex < arr1.length+1 && columnIndex>0){
				System.out.println("Column Name of : "+columnIndex+" is : "+arr1[columnIndex-1]);
				raf.seek(temp);
				return(arr1[columnIndex-1]);
			}
			else
			{
				System.out.println("There is no column of given index.");
				raf.seek(temp);
				return ("error");
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return null;
	}
	String[] sortArrayIndex(String[] arrayForSort) 
	{
			for(int j=0;j<arrayForSort.length;j++)
			{
				int i = getColumnIndex(arrayForSort[j])-1;
				{
					String temp = arrayForSort[i];
					arrayForSort[i] = arrayForSort[j];
					arrayForSort[j] = temp;
				}
		}
		for(int i=0;i<arrayForSort.length;i++)
			
		System.out.println(arrayForSort[i]);
		return arrayForSort;
	}
	
	
	
}